﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JoyeriaProject
{
    public partial class frmPayHistory : Form
    {
        ModeloDatos MD = new ModeloDatos();

        DataTable DT;

        public frmPayHistory()
        {
            InitializeComponent();
        }

        private void btnExits_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void frmPayHistory_Load(object sender, EventArgs e)
        {

            
        }
     
        private void LoadInfo(List<int> CustIDs)
        {

            BindingSource bSource = new BindingSource();
            double Saldos = 0;
            DT = new DataTable();

            DT.Columns.Add("ID");
            DT.Columns.Add("Nombre Completo");
            DT.Columns.Add("Pago Minimo");
            DT.Columns.Add("Ciclo de Pago");
            DT.Columns.Add("Dia de Cobro");
            DT.Columns.Add("Agente Asignado");
            DT.Columns.Add("Monto Comprado");
            DT.Columns.Add("Monto Pagado");
            DT.Columns.Add("Saldo Actual");

            var elements = from c in MD.GetClients()   
                            where (CustIDs.Contains(c.CustomerID))
                            group c by c.CustomerID into g
                            select new
                            {
                                ID = g.Key,

                                NombreCompleto = (from nested in MD.GetClients()
                                                   where(nested.CustomerID == g.Key)
                                                   select nested.Nombre + " " + nested.Apellidos).FirstOrDefault(),
                                PagoMinimo = (from nested in MD.GetClients()
                                               where(nested.CustomerID == g.Key)
                                               select nested.MontoAbono).FirstOrDefault(),

                                CicloPago = (from nested in MD.GetClients()
                                              where(nested.CustomerID == g.Key)
                                              select nested.TipoAbono + " Días").FirstOrDefault(),
                                
                                DiaCobro =  (from nested in MD.GetClients()
                                              where(nested.CustomerID == g.Key)
                                              select nested.DiaCobro.ToString()).FirstOrDefault(),
                               
                                AgenteAsignado = (from nested in MD.GetClients()
                                                   where(nested.CustomerID == g.Key)
                                                   select nested.Agente).FirstOrDefault(),

                                MontoComprado = (from nested in MD.GetComprasPagos()
                                                 where(nested.CustomerID == g.Key)
                                                 select nested.Cargo).Sum(),

                                MontoPagado = (from nested in MD.GetComprasPagos()
                                                 where(nested.CustomerID == g.Key)
                                                 select nested.Abono).Sum(),

                                Saldo = (from nested in MD.GetComprasPagos()
                                         where (nested.CustomerID == g.Key)
                                         group nested by nested.CustomerID into gg
                                         select new {Saldo = (gg.Sum(n => n.Cargo) - gg.Sum(n => n.Abono))}).FirstOrDefault()
                                          
                            };
 

            foreach (var element in elements)
            {
                    DataRow row = DT.NewRow();
               
                    row["ID"] = element.ID;
                    row["Nombre Completo"] = element.NombreCompleto;
                    row["Pago Minimo"] = "$" + element.PagoMinimo;
                    row["Ciclo de Pago"] = element.CicloPago;
                    row["Dia de Cobro"] = (element.DiaCobro == "0" ? "DOMINGO" : element.DiaCobro == "1" ? "LUNES" : element.DiaCobro == "2" ? "MARTES" : element.DiaCobro == "3" ? "MIERCOLES" : element.DiaCobro == "4" ? "JUEVES" : element.DiaCobro == "5" ? "VIERNES" : "SABADO");
                    row["Agente Asignado"] = element.AgenteAsignado;
                    row["Monto Comprado"] = "$" + element.MontoComprado;
                    row["Monto Pagado"] = "$" + element.MontoPagado;
                    row["Saldo Actual"] = "$" + (element.Saldo == null ? "0" : element.Saldo.Saldo.ToString());
                    DT.Rows.Add(row);
                    DT.AcceptChanges();
            }

            bSource.DataSource = DT;
            this.dataGridView.DataSource = bSource;  
        }
 
        private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
             
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            List<int> Clientes;
            dataGridView.DataSource = "";
            int OptionChecked = (rdbTodos.Checked ? 3 : rdbMalos.Checked ? 2 : 1);

          
            if (OptionChecked == 3)//TODOS
            {
                Clientes = MD.GetTodosClientes();
            }
            else if (OptionChecked == 2)// Malos
            {
                Clientes = MD.GetMalosClientes();
            }
            else
            {
                Clientes = MD.GetBuenosClientes();
            }

            LoadInfo(Clientes);
        }

        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {   
            DataRow DR = DT.Rows[e.RowIndex];
         
            frmCustomerDetailGrid CustomerDetails = new frmCustomerDetailGrid(Convert.ToInt32(DR.ItemArray[0]));

            CustomerDetails.ShowDialog();
        }
    }
}
